#ifndef CONSOLE_H
#define CONSOLE_H

#include "comm/types.h"
#include "dev/tty.h"
#include  "ipc/mutex.h"

#define CONSOLE_DISP_ADDR   0xb8000
#define CONSOLE_DISP_END    (0xb8000 + 32*1024)
#define CONSOLE_ROW_MAX     25
#define CONSOLE_COL_MAX     80

#define ASCII_ESC       0x1b
#define ASCII_DEL       0x7F
#define ESC_PARAM_MAX   10

typedef enum _color_t{
    COLOR_Black = 0,
    COLOR_Blue,
    COLOR_Green,
    COLOR_Cyan,
    COLOR_Red,
    COLOR_Magenta,
    COLOR_Brown,
    COLOR_Gray,
    COLOR_DarkGray,
    COLOR_Light_Blue,
    COLOR_Light_Green,
    COLOR_Light_Cyan,
    COLOR_Light_Red,
    COLOR_Light_Magenta,
    COLOR_Yellow,
    COLOR_White,
}color_t;

//表示显存中的字符,一个字节表示asc码,一个字节表示颜色
typedef union _disp_char_t
{
    struct{
        char c;
        char foreground:4;
        char background:3;
    };
    
    uint16_t v;
}disp_char_t;

//ESC 7 8
//ESC [p0;p1m
//表示控制台的结构体
typedef struct _console_t
{
    enum{
        CONSOLE_WRITE_NORMAL,
        CONSOLE_WRITE_ESC,
        CONSOLE_WRITE_SQUARE,
    }write_state;

    disp_char_t* disp_base;     //console的起始地址
    int cursor_row,cursor_col;  //光标所在位置
    int display_rows,display_cols;  //行,列数
    color_t foreground,background;

    int old_cursor_col,old_cursor_row;

    int esc_param[ESC_PARAM_MAX];
    int curr_param_index;

    mutex_t mutex;
}console_t;


int console_init(int idx);
int console_write (tty_t* tty);
void console_close(int console);

void console_select(int idx);

#endif